<?php

namespace Admin\Controller;

use Think\Controller;

class CateController extends CommonController
{
    public function lst()
    {
        $cate=D('cate');
        $res=$cate->query("SELECT * FROM cate");
        $this->assign('res',$res);
        $this->display();
    }

    public function add()//添加
    {
        $cate=D('cate');
//        $res=$cate->query("SELECT * FROM cate");
        if (IS_POST){
            $data['name']=I('post.name');
            if ($cate->create($data)){
                if ($cate->add()){
                    $this->success('添加分类成功！',U('lst'));
                }else{
                    $this->error('添加分类失败！');
                }
            }else{
                $this->error($cate->getError());
            }
            return;
        }
        $this->display();
    }
    public function alter()//修改
    {
        $cate=D('cate');
//        $id=I('id');
//        $res=$cate->query("SELECT * FROM cate WHERE id=$id");
          $res=$cate->find(I('id'));
          $this->assign('res',$res);
        if (IS_POST){
            $data['id']=I('post.id');
            $data['name']=I('post.name');
            if ($cate->create($data)){
                if ($cate->save()){
                    $this->success('修改分类成功！',U('lst'));
                }else{
                    $this->error('修改分类失败！');
                }
            }else{
                $this->error($cate->getError());
            }
            return;
        }
        $this->display();
    }
    public function del()//删除
    {
        $cate=D('cate');
        $id=I('id');
        $res=$cate->query("SELECT * FROM cate WHERE id=$id");
        if ($cate->delete($id)){
            $this->success('删除成功！',U('lst'));
        }else{
            $this->error('删除失败！');
        }
    }

}

